/**
 *  DBEXEL is a Database Backed & Web-Based Worksheet and chart management program. 
 *  It has been influenced by Excel.
 *  For questions or suggestions please contact the developper at ( Development@Gandomi.com )
 *  Copyright (C) 2011 Baubak Gandomi   
 *
 *	This file is part of the application DBEXEL
 *
 *   DBEXEL is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   DBEXEL is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package dbexel.model.dao;

import dbexel.model.mapping.AttributeValue;
import dbexel.model.mapping.WorkSheet_Entry;

/**
 * @author Baubak
 *
 */
public class WorkSheetEntryDaoImpl extends JPAResourceDao implements WorkSheetEntryDao {

	/* (non-Javadoc)
	 * @see dbexel.model.dao.WorkSheetEntryDao#createEntry(dbexel.model.mapping.WorkSheet_Entry)
	 */
	@Override
	public void createWorkSheetEntry(WorkSheet_Entry in_wsEntry) {
		beginTransaction();

		for (AttributeValue lt_attAttributeValue : in_wsEntry.getAttributeValues()) {
			getEm().persist(lt_attAttributeValue);
		}
		//getEm().persist(in_wsEntry.getWorkSheet());
		getEm().persist(in_wsEntry);
		commitTransaction();
		
	}

	/* (non-Javadoc)
	 * @see dbexel.model.dao.WorkSheetEntryDao#fetchWorkSheetEntry(java.lang.Long)
	 */
	@Override
	public WorkSheet_Entry fetchWorkSheetEntry(Long entryId) {
		return getEm().find(WorkSheet_Entry.class, entryId);
		
	}

	/* (non-Javadoc)
	 * @see dbexel.model.dao.WorkSheetEntryDao#updateWorkSheet(dbexel.model.mapping.WorkSheet_Entry)
	 */
	@Override
	public void updateWorkSheet(WorkSheet_Entry in_wsEntry) {
		beginTransaction();
		getEm().persist(in_wsEntry);
		commitTransaction();
		
		
	}

	/* (non-Javadoc)
	 * @see dbexel.model.dao.WorkSheetEntryDao#deleteWorkSheetEntry(dbexel.model.mapping.WorkSheet_Entry)
	 */
	@Override
	public void deleteWorkSheetEntry(WorkSheet_Entry wsEntry) {
		//Remove entry from worksheet
		wsEntry.getWorkSheet().getEntries().remove(wsEntry);
		
		//aslo we need to remove all entries
		
		beginTransaction();
		getEm().remove(wsEntry);
		commitTransaction();
		
	}

}
